package com.study.goal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import com.study.goal.domain.IntegralExchangeItem;
import com.study.goal.domain.bo.IntegralExchangeItemBo;
import com.study.goal.domain.vo.IntegralExchangeItemVo;
import com.study.goal.mapper.IntegralExchangeItemMapper;
import com.study.goal.service.IIntegralExchangeItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 积分兑换Service业务层处理
 *
 * @author MT
 * @date 2025-09-03
 */
@RequiredArgsConstructor
@Service
public class IntegralExchangeItemServiceImpl implements IIntegralExchangeItemService {

    private final IntegralExchangeItemMapper baseMapper;

    /**
     * 查询积分兑换
     */
    @Override
    public IntegralExchangeItemVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询积分兑换列表
     */
    @Override
    public TableDataInfo<IntegralExchangeItemVo> queryPageList(IntegralExchangeItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IntegralExchangeItem> lqw = buildQueryWrapper(bo);
        Page<IntegralExchangeItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询积分兑换列表
     */
    @Override
    public List<IntegralExchangeItemVo> queryList(IntegralExchangeItemBo bo) {
        LambdaQueryWrapper<IntegralExchangeItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IntegralExchangeItem> buildQueryWrapper(IntegralExchangeItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IntegralExchangeItem> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), IntegralExchangeItem::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getItemType()), IntegralExchangeItem::getItemType, bo.getItemType());
        lqw.eq(bo.getIntegralPrice() != null, IntegralExchangeItem::getIntegralPrice, bo.getIntegralPrice());
        lqw.eq(bo.getStock() != null, IntegralExchangeItem::getStock, bo.getStock());
        lqw.eq(bo.getLimitPerUser() != null, IntegralExchangeItem::getLimitPerUser, bo.getLimitPerUser());
        lqw.eq(bo.getStartTime() != null, IntegralExchangeItem::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, IntegralExchangeItem::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), IntegralExchangeItem::getStatus, bo.getStatus());
        lqw.eq(bo.getSortOrder() != null, IntegralExchangeItem::getSortOrder, bo.getSortOrder());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), IntegralExchangeItem::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getImageUrl()), IntegralExchangeItem::getImageUrl, bo.getImageUrl());
        return lqw;
    }

    /**
     * 新增积分兑换
     */
    @Override
    public Boolean insertByBo(IntegralExchangeItemBo bo) {
        IntegralExchangeItem add = BeanUtil.toBean(bo, IntegralExchangeItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改积分兑换
     */
    @Override
    public Boolean updateByBo(IntegralExchangeItemBo bo) {
        IntegralExchangeItem update = BeanUtil.toBean(bo, IntegralExchangeItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IntegralExchangeItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除积分兑换
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
